package com.wmh.baseservice.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.user.pojo.UsEvaluate;
import com.wmh.baseservice.user.mapper.UsEvaluateMapper;
import com.wmh.baseservice.user.pojo.UsGroupGoodsOrder;
import com.wmh.baseservice.user.pojo.UsVegetableOrder;
import com.wmh.baseservice.user.service.UsEvaluateService;
import com.wmh.baseservice.common.mybatisplus.methods.CommonServiceImpl;
import com.wmh.baseservice.user.service.UsGroupGoodsOrderService;
import com.wmh.baseservice.user.service.UsVegetableOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author wmh generator
 * @since 2022-02-02
 */
@Service
public class UsEvaluateServiceImpl extends CommonServiceImpl<UsEvaluateMapper, UsEvaluate> implements UsEvaluateService {

    @Resource
    private UsGroupGoodsOrderService usGroupGoodsOrderService;
    @Resource
    private UsVegetableOrderService usVegetableOrderService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(UsEvaluate usEvaluate) {
        if (!save(usEvaluate)) {
            throw new ServiceException();
        }
        //修改对应订单状态
        if (usEvaluate.getType() == 4) {
            UsGroupGoodsOrder usGroupGoodsOrder = usGroupGoodsOrderService.getOneForUpdate(new QueryWrapper<UsGroupGoodsOrder>().eq(UsGroupGoodsOrder.ORDER_NO, usEvaluate.getOrderNum()));
            usGroupGoodsOrder.setStatus(6);
            if (!usGroupGoodsOrderService.updateById(usGroupGoodsOrder)) {
                throw new ServiceException();
            }

        } else if (usEvaluate.getType() == 1) {
            UsVegetableOrder usVegetableOrder = usVegetableOrderService.getOneForUpdate(new QueryWrapper<UsVegetableOrder>().eq(UsVegetableOrder.ORDER_NUM, usEvaluate.getOrderNum()));
            usVegetableOrder.setStatus(4);
            if (!usVegetableOrderService.updateById(usVegetableOrder)) {
                throw new ServiceException();
            }

        }
    }
}
